const app = getApp();
const api = require("../../utils/api");

Page({
    data: {
        couponList: [],

        showQuantity: false, //兑换数量弹窗
        couponSelected: '', //选择要购买的购物券商品
        quantity: 1,
        total: 0,
    },
    onLoad(options) {

    },
    onShow() {
        this.getCouponData();
    },

    getCouponData() { //获取购物券列表
        api.post('/coupon/product/list')
            .then(res => {
                let list = res;
                list.forEach(item => {
                    // item.quantity = 0;
                    item.couponPriceShow = item.couponPrice.toFixed(2);
                });

                this.setData({
                    couponList: list
                });
            })
    },

    //购买（设置数量）弹窗
    openQuantity(e) { //打开设置数量弹窗
        if(app.globalData.userInfo.phone == '') {
            //未授权手机号，需要先去授权
            wx.showModal({
                title: '温馨提示',
                content: `请先完成会员注册！`,
                success: res => {
                    if(res.confirm) {
                        wx.navigateTo({
                            url: '/pages/login/login?dir=1',
                        });
                    }
                }
            });
            return;
        } else {
            this.setData({
                couponSelected: e.currentTarget.dataset.item,
                showQuantity: true
            });
            this.dealSummary();
        }
    },
    closeQuantity() { //关闭设置数量弹窗
        this.setData({
            showQuantity: false,
            quantity: 1
        });
    },

    quantityPlus() { //数量加
        let num = this.data.quantity;
        this.setData({
            quantity: num + 1
        });
        this.dealSummary();
    },
    quantityMinus() { //数量减
        if(this.data.quantity == 1) {
            return;
        }
        let num = this.data.quantity;
        this.setData({
            quantity: num - 1
        });
        this.dealSummary();
    },
    dealSummary() {
        this.setData({
            total: (this.data.couponSelected.couponPrice * this.data.quantity).toFixed(2)
        });
    },
    submitOrder() {
        let params = {
            productId: this.data.couponSelected.id,
            couponPrice: this.data.couponSelected.couponPrice, //销售金额
            couponCredits: this.data.couponSelected.couponCredits, //销售价格(积分)
            orderQuantity: this.data.quantity,
            orderAmount: (this.data.couponSelected.couponPrice * this.data.quantity).toFixed(2), //订单总金额
            orderAmountCredit: this.data.couponSelected.couponCredits * this.data.quantity, //订单总金额（积分）
            payAmount: (this.data.couponSelected.couponPrice * this.data.quantity).toFixed(2), //应付金额
            payAmountCredits: this.data.couponSelected.couponCredits * this.data.quantity, //实付金额（积分）
        }
        api.post('/coupon/order/add', params)
            .then(res => {
                wx.showToast({
                    title: '下单成功，跳转支付中...',
                    icon: 'none'
                });
                setTimeout(() => {
                    wx.redirectTo({
                        url: `/pages/orderPay/orderPay?id=${res}&type=1`,
                    });
                }, 1000);
                this.closeQuantity();
            })
    }
})